package com.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.sys.entity.SysUserRole;
import com.sys.service.SysUserRoleService;
import com.bhudy.base.QueryFilter;
import com.bhudy.enums.Relation;
import com.bhudy.exception.SysException;
import com.bhudy.exception.SysExceptionCode;
import com.bhudy.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author bhudy
 * @Description 用户关联角色controller
 * @Date 2019-10-30 23:09:37
 */
@RestController
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 获取sysRole的数据
     *
     * @param page   页数{1}
     * @param limit  显示数量{10}
     * @param userId 用户Id 1
     * @return sysRole的list
     */
    @GetMapping(value = "/api/v1/sysUserRoles")
    public ResponseResult getAll(Integer page, Integer limit, String userId) {
        QueryFilter queryFilter = new QueryFilter();
        queryFilter.clear().and("status", Relation.NOT_EQUAL, 1);
        if (page != null && limit != null) queryFilter.limit(--page, limit);
        if (userId != null) queryFilter.and("userId", userId);
        return ResponseResult.printMapJson(0, sysUserRoleService.count(queryFilter), sysUserRoleService.findList(queryFilter));
    }

    @GetMapping(value = "/api/v1/sysUserRole")
    public ResponseResult get(Long id) {
        return ResponseResult.printMapJson(0, sysUserRoleService.get(id));
    }

    @PostMapping(value = "/api/v1/sysUserRoles")
    public ResponseResult insert(@RequestBody String json, Long userId) {
        if (userId == null) throw new SysException(SysExceptionCode.CODE_40003);
        return ResponseResult.printMapJson(0, sysUserRoleService.createSysUserRoles(JSONObject.parseArray(json, SysUserRole.class), userId));
    }

    @PutMapping(value = "/api/v1/sysUserRole")
    public ResponseResult update(SysUserRole sysUserRole) {
        return ResponseResult.printMapJson(0, sysUserRoleService.update(sysUserRole));
    }

    @DeleteMapping(value = "/api/v1/sysUserRoles")
    public ResponseResult delete(Long id) {
        return ResponseResult.printMapJson(0, sysUserRoleService.delete(id));
    }

}
